package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenuByUsername")
    public Result getMenuByUsername(String username){
        try {
            List<Menu> menus= menuService.getMenuByUsername(username);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,menus);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_SUCCESS);
        }
    }


    //菜单管理分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =  menuService.findPage(queryPageBean);
        System.out.println("返回的分页结果=="+pageResult);
        return  pageResult;
    }


    //菜单管理新增项:
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //菜单项编辑,表单回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu =  menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑菜单项.
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    //删除菜单项:
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }



    //角色需要的菜单:
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> list =  menuService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
